﻿namespace dogd
{
	using System.IO;
	using System.Xml.Serialization;

	/// <summary>
	/// Simple persistence of class to and from xml format
	/// </summary>
	public static class XmlPersistence
	{
		/// <summary>
		/// Creates a new instance of T from a text stream
		/// </summary>
		/// <typeparam name="T"></typeparam>
		/// <param name="stream"></param>
		/// <returns></returns>
		[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter", Justification = "As intended")]
		public static T LoadFrom<T>(TextReader stream)
		{
			XmlSerializer xml = new XmlSerializer(typeof(T));

			return (T)xml.Deserialize(stream);
		}

		/// <summary>
		/// Write an instance of T to a text stream
		/// </summary>
		/// <typeparam name="T"></typeparam>
		/// <param name="stream"></param>
		/// <param name="persist"></param>
		[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter", Justification = "As intended")]
		public static void SaveTo<T>(TextWriter stream, T persist)
		{
			XmlSerializer xml = new XmlSerializer(typeof(T));
			xml.Serialize(stream, persist);
		}
	}
}
